package com.Object01.SmallChange.OOP;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 完成零钱通的各个功能
 * 使用OOP面向对象编程
 */
public class SmallChangeSysOOP {
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";
    //零钱通明细
    //用字符串拼接
    String details = "-----------零钱通明细--------------";
    //收益入账和消费功能
    double money = 0;
    double balance = 0;
    //获取当前日期
    Date date = null;//表示日期
    //用以下方法进行日期格式化
    SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");//可以用来对日期的格式化
    String name = "";
    //退出
    char c = ' ';

    public void mainMenu() {
        do {
            System.out.println("\n--------零钱通菜单----------");
            System.out.println("\t1.零钱通明细");
            System.out.println("\t2.收益入账");
            System.out.println("\t3.消   费");
            System.out.println("\t4.退   出");
            System.out.print("请选择（1-4）：");
            key = scanner.next();
            switch (key) {
                case "1":
                    this.detail();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.Pay();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("输入错误！（1-4）");
            }
        } while (loop);
    }

    public void detail() {
        System.out.println(details);
    }

    public void income() {
        System.out.println("------收益入账------");
        System.out.println("入账金额：");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("输入的入账金额有误！");
            return;
        }
        balance += money;
        //得到时间：得到当前日期
        date = new Date();//对格式进行处理
        details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    public void Pay() {
        System.out.println("------消----费------");
        System.out.println("请输入消费的商家名称：");
        name = scanner.next();
        System.out.println("请输入消费的价格：");
        money = scanner.nextInt();
        if (money > balance || money <= 0) {
            System.out.println("消费金额不可高于余额！");
            return;
        }
        balance -= money;
        date = new Date();
        details += "\n" + name + "\t-" + money + "\t" + sdf.format(date) + "\t余额：" + balance;
    }

    public void exit() {
        System.out.println("------退----出------");
//                    if (c == 'y'||c == 'Y') {
//                        System.out.println("-------程序退出------");
//                        loop = false;
//                    } else {
//                        System.out.println("程序继续~");
//                    }
        while (true) {
            System.out.println("确定要退出程序吗？");
            c = scanner.next().charAt(0);
            if (c == 'y' || c == 'n') {
                break;
            }
        }
        if (c == 'y') {
            loop = false;
            System.out.println("--------程序退出---------");
        }
    }
}
